import { createRouter, createWebHistory } from "vue-router";
import Login from "../views/Login.vue";
import Layout from "../views/Layout.vue";
import UserInfo from "../views/user/UserInfo.vue";
import UserList from "../views/user/UserList.vue";
import ResetPassword from "../views/user/ResetPassword.vue";
import HouseManage from "../views/house/HouseManage.vue";
import Index from "../views/index/Index.vue";
import BookingManage from "../views/booking/BookingManage.vue";
import BlacklistManage from "../views/blacklist/BlacklistManage.vue";
import LogManage from "../views/log/LogManage.vue";
import BookingCreate from '../views/booking/BookingCreate.vue'
import MyBookings from '../views/booking/MyBookings.vue'
import BlacklistAppeal from "../views/blacklist/BlacklistAppeal.vue";
import DeviceReport  from "../views/booking/DeviceReport.vue";
import DeviceReportManage from "../views/booking/DeviceReportManage.vue";
import { useUserInfoStore } from "../stores/userInfo";
import { ElMessage } from 'element-plus'
import BookingSchedule    from '../views/booking/BookingSchedule.vue'
import StatisticsReport from '../views/StatisticsReport.vue';


const routes = [
  { path: "/login", component: Login },
  {
    path: "/",
    redirect: "/login",
    component: Layout,
    children: [
      { path: "/index", component: Index },
      { path: "/user/list", component: UserList },
      { path: "/user/info", component: UserInfo },
      { path: "/user/resetPassword", component: ResetPassword },
      {
        path: '/admin/manage',
        name: 'adminManage',
        component: () => import('../admin/AdminManage.vue'),
        meta: {
          requiresAuth: true,
          requiredRoles: [2] // 仅超级管理员可访问
        }
      },
      {
        path: "/house/list",
        component: HouseManage,
        meta: { title: "会议室信息列表" }
      },
      {
        path: "/house",
        name: "HouseAdd",
        component: () => import('../views/house/HouseAdd.vue'),
        meta: { title: "会议室信息管理" }
      },
      { path: "/booking/manage", component: BookingManage },
      { path: "/blacklist/list", component: BlacklistManage },
      { path: "/log/list", component: LogManage },
      {
        path: "/user/list",
        name: "user",
        component: UserList,
        meta: { requiresAdmin: true }
      },
      {
        path: "/booking/manage",
        name: "booking",
        component: BookingManage,
        meta: { requiresAdmin: true }
      },
      {
        path: "/booking/create",
        name: "BookingCreate",
        component: BookingCreate,
        meta: { title: "会议室预约", requiresAuth: true }
      },
      {
        path: "/booking/my",
        name: "MyBookings",
        component: MyBookings,
        meta: { title: "我的预约", requiresAuth: true }
      },
      // 成员 & 管理：会议排期查询
      {
        path: 'booking/schedule',
        name: 'BookingSchedule',
        component: BookingSchedule,
        meta: { title: '会议排期查询', roles: ['member', 'admin'] }
      },
      {
        path: '/blacklist',
        name: 'BlacklistManage',
        component: () => import('../views/blacklist/BlacklistManage.vue')
      },
      {
        path: '/user/reset-password',
        name: 'ResetPassword',
        component: () => import('../views/user/ResetPassword.vue')
      },
      {
        path: "blacklist/appeal",
        name: "BlacklistAppeal",
        component: BlacklistAppeal,
        meta: { title: "黑名单申诉", requiresAuth: true },
      },
      // 成员端：提交报修
      {
        path: 'booking/device-report',
        name: 'DeviceReport',
        component: DeviceReport,
        meta: { title: '设备报修', roles: ['member'] }
      },
      // 管理端：处理报修
      {
        path: 'booking/device-report/manage',
        name: 'DeviceReportManage',
        component: DeviceReportManage,
        meta: { title: '设备报修处理', roles: ['admin'] }
      },
      {
        path: '/statistics-report',
        name: 'StatisticsReport',
        component: StatisticsReport,
        meta: { title: '统计分析与报表', requiredRoles: [1, 2] }
      }
    ],
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 修改路由守卫
router.beforeEach((to, from, next) => {
  const userInfoStore = useUserInfoStore();

  // 检查路由是否需要管理员权限
  const requiresAdmin = to.matched.some(record => record.meta.requiresAdmin);

  // 使用真实角色值进行验证
  const realRole = userInfoStore.userInfo.role || 0;

  if (requiresAdmin && realRole !== 1) {
    ElMessage.warning('您没有访问此页面的权限');
    next('/index');
  } else {
    next();
  }
});
export default router